<script setup lang="ts">
import { onMounted } from "vue";
import { formattedDate } from "@/utils/formatDate.ts";
import { Delete, Edit, InfoFilled } from "@element-plus/icons-vue";
import Dialog from "@/view/Identity/dialog.vue";
import Pager from "@/components/Pager.vue";
import useUserIdentity from "@/view/Identity/utils/hook.tsx";

const {
  total,
  pagerParams,
  identityList,
  dialog,
  addIdentity,
  getIdentityList,
  EditIdentity,
  deleteIdentity,
  handleCurrentPageChange,
} = useUserIdentity()

onMounted(() => {
  getIdentityList()
})
</script>

<template>
  <el-card class="box-card">
    <div class="card-identity-title">
      <el-button type="primary" @click="addIdentity">添加身份</el-button>
    </div>
    <div class="card-identity-table">
      <el-table
        border
        ref="multipleTableRef"
        :data="identityList"
        style="width: 100%"
      >
        <el-table-column label="#" width="100" align="center" type="index"/>
        <el-table-column prop="_id" align="center" label="id"/>
        <el-table-column prop="name" align="center" label="名称"/>
        <el-table-column align="center" label="控制" width="260">
          <template #default="{row}">
            <div class="operator-btn">
              <el-button type="primary" size="default" @click="EditIdentity(row)" :icon="Edit as any" circle/>
              <el-popconfirm
                width="200"
                @confirm="deleteIdentity(row)"
                confirm-button-text="确定"
                cancel-button-text="取消"
                :icon="InfoFilled as any"
                icon-color="#f36b6c"
                title="您确定删除此身份吗?"
              >
                <template #reference>
                  <el-button type="danger" size="default" :icon="Delete as any" circle/>
                </template>
              </el-popconfirm>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <Pager :size="pagerParams.limit"
           :total="total"
           :currentPage="pagerParams.page"
           @currentPageChange="handleCurrentPageChange"
    />

    <Dialog ref="dialog" @getIdentityList="getIdentityList"/>
  </el-card>
</template>

<style scoped lang="scss">
.box-card {
  padding: 0 50px;

  .card-identity-title {
    display: flex;
    justify-content: flex-end;
  }

  .card-identity-table {
    margin-top: 20px;
  }
}
</style>
